/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.global.web;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author ctrosch
 */
@ManagedBean
@SessionScoped
public class PruebaMailBean implements Serializable {

    @Resource(name = "intercapsistemas@gmail.com")
    private Session mailSession;

    public void sendMessage() {
        Message msg = new MimeMessage(mailSession);
        try {

          msg.setFrom(new InternetAddress("intercapsistemas@gmail.com","Intercap S.R.L."));
          msg.setSubject("Prueba de envio");
          msg.setRecipient(RecipientType.TO, new InternetAddress("claudiotrosch@gmail.com","Claudio Trosch"));

          MimeBodyPart messageBodyPart = new MimeBodyPart();

          String contenido = "<h2>Hola que tal</h2>";

          messageBodyPart.setContent(contenido,"text/html");
          Multipart multipart = new MimeMultipart();
          multipart.addBodyPart(messageBodyPart);
          /**
          for( String rutaAdjunto : adjuntos ){
            messageBodyPart = new MimeBodyPart();
            File f = new File(rutaAdjunto);

            if( f.exists() ){

                DataSource source = new FileDataSource( rutaAdjunto );
                messageBodyPart.setDataHandler( new DataHandler(source) );
                messageBodyPart.setFileName( f.getName() );

                multipart.addBodyPart(messageBodyPart);

            }

        }
        */

        //Se junta el mensaje y los archivos adjuntos
        msg.setContent(multipart);
        //Se env&iacute;a el e-mail
        //msg.setText("Hello Claudio");
        mailSession.getTransport().send(msg);
        
        }
        catch(MessagingException me) {
            me.printStackTrace();
        }
        catch(UnsupportedEncodingException uee) {
            uee.printStackTrace();
        }
    }





}
